/**
 * Copyright (c) 2007, Blue Hole Software. All rights reserved.
 * Code licensed under the Apache 2.0 License:
 * http://www.apache.org/licenses/
 */
package org.bhf.security.common;

import javax.security.auth.Subject;
import java.io.Serializable;
import java.util.Set;

/**
 * Public credential for a Login ID.
 * Each authenticated <code>Subject</code> carries a <code>LoginID</code>
 * as a public credential.
 */
public final class LoginID implements Serializable
{
    private static final long           serialVersionUID    = 7930732926638008763L;
    private static final Class<LoginID> THIS_CLASS          = LoginID.class;

    private final String ID;

    /**
     * Create a <code>LoginID</code> with the given name.
     *
     * @param ID the Login ID.  Must not be <code>null</code>.
     *
     * @throws IllegalArgumentException if <code>loginID == null</code>.
     */
    public LoginID( final String ID )
    {
        if( ID == null )
            throw new IllegalArgumentException( "ID == null" );

        this.ID = ID;
    }

    /**
     * Returns the ID of this <code>LoginID</code>.
     *
     * @return the ID of this <code>LoginID</code>.
     */
    public String               getID()
    {
        return ID;
    }


    /**
     * Returns the Login ID.
     *
     * @return the Login ID.  Will not return <code>null</code>.
     */
    public String               getName()
    {
        return ID;
    }

    /**
     * Convenience method to return the <code>LoginID</code> in a
     * <code>Subject</code>.
     *
     * @param subject The <code>Subject</code> from which to extract the
     *      <code>LoginID</code>.  Must not be <code>null</code>.
     * @return The <code>LoginID</code> of the given <code>Subject</code>
     * @throws IllegalArgumentException if <code>subject == null</code>
     *      or if <code>subject</code> does not contain exactly one
     *      <code>LoginID</code> as a public credential.
     */
    public static LoginID      getLoginID( Subject subject )
    {
        if( subject == null )
            throw new IllegalArgumentException( "subject == null" );

        Set<LoginID> set = subject.getPublicCredentials( THIS_CLASS );

        if( set.size() != 1 )
            throw new IllegalArgumentException( "subject.getPublicCredentials( LoginID.class ).size() != 1" );

        return set.iterator().next();
    }

    /**
     * Convenience method to return the <code>LoginID</code> of the
     * <code>Subject</code> associated with the current thread.
     *
     * @return The <code>LoginID</code> of the contextual <code>Subject</code>
     * @throws IllegalArgumentException if <code>subject</code> does not
     *      contain exactly one <code>LoginID</code> as a public credential
     *      or if there is no code>Subject</code> associated with the
     *      current thread.
     */
    public static LoginID      getLoginID()
    {
        return getLoginID( ContextSubject.getContextSubject() );
    }

    /**
     * Returns the hash code based on first and last name.
     *
     * @return the hash code.
     */
    public int                  hashCode()
    {
        return ID.hashCode();
    }

    /**
     * Equality based on first and last name.
     *
     * @param o Object against which to compare
     *
     * @return <code>true</code> iff <code>o</code> is a <code>LoginID</code>
     *      with the same first and last names.
     */
    public boolean              equals( Object o )
    {
        try
        {
            LoginID that = (LoginID)o;
            return that.ID.equals( ID );
        }
        catch( Exception e )
        {
            return false;
        }
    }
}


